<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Canvas Viewport</title>
    <style>
      #container {
        width: 414px;
        height: 414px;
        position: relative;
        border: 1px solid black;
        overflow: hidden;
      }
      canvas {
        display: block;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <canvas id="viewport"></canvas>
    </div>

    <script>
      const container = document.getElementById('container');
      const canvas = document.getElementById('viewport');
      const ctx = canvas.getContext('2d');

      // Set canvas size to match the container size
      canvas.width = container.clientWidth;
      canvas.height = container.clientHeight;

      const image = new Image();
      image.src = 'src/assets/map2.jpg'; // Replace with your image URL

      let viewport = {
        x: 0,
        y: 0,
        width: canvas.width,
        height: canvas.height,
      };

      let isDragging = false;
      let dragStart = { x: 0, y: 0 };

      image.onload = () => {
        drawViewport();
      };

      function drawViewport() {
        // Clear canvas
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        // Calculate the source rectangle on the image
        const srcX =
          -(viewport.x / canvas.width) * image.width + canvas.width / 2;
        const srcY =
          -(viewport.y / canvas.height) * image.height + canvas.height / 2;
        const srcWidth = (viewport.width / canvas.width) * image.width;
        const srcHeight = (viewport.height / canvas.height) * image.height;

        // Ensure the source rectangle is within the image bounds
        const actualSrcX = Math.max(0, Math.min(srcX, image.width - srcWidth));
        const actualSrcY = Math.max(
          0,
          Math.min(srcY, image.height - srcHeight)
        );

        // Draw the portion of the image onto the canvas
        ctx.drawImage(
          image,
          actualSrcX,
          actualSrcY,
          srcWidth,
          srcHeight,
          0,
          0,
          canvas.width,
          canvas.height
        );
      }

      // Mouse events for dragging
      canvas.addEventListener('mousedown', (e) => {
        isDragging = true;
        dragStart.x = e.clientX - viewport.x;
        dragStart.y = e.clientY - viewport.y;
      });

      canvas.addEventListener('mousemove', (e) => {
        if (isDragging) {
          viewport.x = e.clientX - dragStart.x;
          viewport.y = e.clientY - dragStart.y;

          // Constrain the viewport to stay within the image bounds
          const maxX = image.width - canvas.width;
          const maxY = image.height - canvas.height;

          viewport.x = Math.max(0, Math.min(viewport.x, maxX));
          viewport.y = Math.max(0, Math.min(viewport.y, maxY));

          drawViewport();
        }
      });

      canvas.addEventListener('mouseup', () => {
        isDragging = false;
      });

      canvas.addEventListener('mouseleave', () => {
        isDragging = false;
      });
    </script>
  </body>
</html>
